import React, { useEffect, useState } from "react";
import {AwardRecordVO} from "@/types/AwardRecordVO";
import {queryAwardRecord} from "@/apis";
interface Request {
    activityId?: number;
    pageNum?: number;
    pageSize?: number;
    /**
     * 用户ID
     */
    userId?: string;
    [property: string]: any;
}


export function UserAwardRecordList({refresh}) {

    const [records, setRecords] = useState<Request[]>([]);
    const [currentPage, setCurrentPage] = useState(1);
    const pageSize = 5;

    const queryAwardRecordHandle = async () => {
        const queryParams = new URLSearchParams(window.location.search);
        // 查询账户数据
        const result = await queryAwardRecord(String(queryParams.get('userId')), Number(queryParams.get('activityId')), queryParams.get("pageNum"));
        const {code, info, data}: { code: string; info: string; data: AwardRecordVO } = await result.json();

        if (code != "0000") {
            window.alert("查询活动账户额度，接口调用失败 code:" + code + " info:" + info)
            return;
        }

        // 日可抽奖额度
        setRecords(data)
    }

    useEffect(() => {
        queryAwardRecordHandle().then(r => {
        });
    }, [refresh])


    const handlePageChange = (newPage: number) => {
        setCurrentPage(newPage);
    };

    return (
        <div>
            <h2 className="text-xl font-bold mb-4">中奖记录</h2>
            <ul>
                {records.map((record) => (
                    <li key={record.raffleOrderId} className="border-b p-2">
                        <p><strong>编号:</strong> {record.raffleOrderId}</p>
                        <p><strong>奖品名称:</strong> {record.awardTitle}</p>
                        <p><strong>中奖时间:</strong> {record.awardTime}</p>
                    </li>
                ))}
            </ul>
            <div className="flex justify-between mt-4">
                <button
                    onClick={() => handlePageChange(currentPage - 1)}
                    disabled={currentPage === 1}
                    className="px-4 py-2 bg-blue-500 text-white rounded disabled:bg-gray-300"
                >
                    上一页
                </button>
                <button
                    onClick={() => handlePageChange(currentPage + 1)}
                    className="px-4 py-2 bg-blue-500 text-white rounded"
                >
                    下一页
                </button>
            </div>
        </div>
    );

}
